clear all;
clc;

load mnist_train;

s = 400;
c = 10;
Y = yL';
clear yL;

nump = c*(c-1)/2;
sz = zeros(c,1);
idx = cell(10,1);
for i = 1:c
    idx{i}=find(Y==(i-1));
    sz(i)=size(idx{i},1);
end

fval = zeros(c,c);

V = xL';
clear xL;

for i = 1:c
    for j = i+1:c
        ix = [idx{i};idx{j}];
        Yt = [ones(sz(i),1);-ones(sz(j),1)];
        l = sz(i)+sz(j);
        P = repmat(Yt,1,s).*V(ix,1:s);
        cvx_begin
        cvx_solver sedumi
            variable x(s,1)
            variable b
            variable y(l,1) nonnegative
            minimize (sum(y));
            P*x+b*Yt+y >= ones(l,1);
        cvx_end
        fval(i,j) = cvx_optval;
    end
end

save('res/featestpair','fval');